﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

using FXnaEngine.Components;

namespace FXnaEngine.Graphics
{
   public class FLoadedModel:FModel
    {

       public FLoadedModel(Model model)
       {
           this.model = model;
       }



       Model model;

       public Model Model
       {
           get { return model; }
           set { model = value; }
       }




       public override void RenderCore(Graphics.FGraphicsSystem graphics,FRendererComponent renderer)
       {
           base.RenderCore(graphics,renderer);



           foreach (ModelMesh mesh in model.Meshes)
           {
               Matrix world = mesh.ParentBone.Transform *renderer.ParentEntity.Transform.WorldMatrix;

               BoundingSphere b = mesh.BoundingSphere.Transform(world);


               //如果不在相机可视范围内，就不用绘制了

               if (!b.Intersects(graphics.CurrentCamera.ViewFrustum))
               {
                   continue;

               }


               foreach (ModelMeshPart part in mesh.MeshParts)
               {

                   BasicEffect myBasicEffect = part.Effect as BasicEffect;

                   myBasicEffect.World = world;

                   myBasicEffect.View = graphics.CurrentCamera.ViewMatrix;
                   myBasicEffect.Projection = graphics.CurrentCamera.ProjectionMatrix;

                   myBasicEffect.TextureEnabled = true;

                   foreach (EffectPass pass in myBasicEffect.CurrentTechnique.Passes)
                   {
                       pass.Apply();


                       graphics.GraphicsDevice.Indices = part.IndexBuffer;
                       graphics.GraphicsDevice.SetVertexBuffer(part.VertexBuffer);
                       graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.VertexOffset, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount);




                   }






               }



           }



       }



    }
}
